package com.chianghao.rock.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chianghao.core.web.R;
import com.chianghao.rock.Constant;
import com.chianghao.rock.admin.system.organization.dto.OrganizationAddDto;
import com.chianghao.rock.admin.system.organization.dto.OrganizationModifyDto;
import com.chianghao.rock.admin.system.organization.vo.OrganizationTreeVo;
import com.chianghao.rock.admin.system.organization.vo.OrganizationVo;
import com.chianghao.rock.common.SequenceService;
import com.chianghao.rock.system.entity.Organization;
import com.chianghao.rock.system.service.organization.OrganizationService;
import com.chianghao.rock.utils.TreeUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("system/organization")
@Api(value = "部门", tags = "部门")
public class OrganizationController {

    @Autowired
    OrganizationService organizationService;

    @Autowired
    SequenceService sequenceService;

	@PostMapping("add")
	@ApiOperation(value="添加组织结构")
	public R<Boolean> add(@Validated OrganizationAddDto dto) {
		Organization organization = toEntity(dto);
		Long id  = sequenceService.getNextId();
		organization.setId(id);
		organizationService.insert(organization);
		return R.getSuccess(true);
	}

    @PostMapping("modify")
    @ApiOperation(value="修改组织结构")
    public R<Boolean> modify(@Validated OrganizationModifyDto dto) {
    	Organization organization = toEntity(dto);
		organizationService.doModifyOrganization(organization);
    	return R.getSuccess(true);
	}

    @DeleteMapping("delete")
    @ApiOperation(value="删除组织结构")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true, dataType = "String")
    public R<Boolean> delete(@NotBlank(message="error:id,not.null") Long id) {
    	organizationService.doDelete(id);
    	return R.getSuccess(true);
    }

    @GetMapping("detail")
    @ApiOperation(value="组织结构明细")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true, dataType = "String")
    public R<OrganizationVo> detail(@NotBlank(message="error:id,not.null") Long id) {
    	Organization  bean = organizationService.queryOneById(id);
    	return R.getSuccess(toVo(bean));
    }

    @GetMapping("children")
    @ApiOperation(value="获取子节点")
    @ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true, dataType = "String")
    public R<List<OrganizationVo>> query(@NotBlank(message="error:id,not.null") Long id) {
    	List<Organization> List = organizationService.queryChildren(id);
    	return R.getSuccess(toListVo(List));
    }

    @GetMapping("tree")
    @ApiOperation(value="获取树形结构")
    public R<List<OrganizationTreeVo>> queryPageList() {
    	List<Organization> List = organizationService.query(null);
    	List<OrganizationTreeVo> tree  = TreeUtils.toTreeDto(OrganizationTreeVo.class, List, Constant.rootId);
    	return R.getSuccess(tree);
    }
    
    
    private OrganizationVo toVo(Organization bean) {
    	OrganizationVo vo = new OrganizationVo();
    	BeanUtils.copyProperties(bean, vo);
    	return vo;
    }
    
    private List<OrganizationVo> toListVo(List<Organization> list){
    	List<OrganizationVo> array = list.stream().map(item->toVo(item)).collect(Collectors.toList());
    	return array;
    }

    
    private Organization toEntity(OrganizationModifyDto dto) {
    	Organization bean = new Organization();
    	BeanUtils.copyProperties(dto, bean);
    	return bean;
    }
    
    private Organization toEntity(OrganizationAddDto dto) {
    	Organization bean = new Organization();
    	BeanUtils.copyProperties(dto, bean);
    	return bean;
    }

}